<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/22 0022
 * Time: 10:10
 */

namespace app\admin\controller;


use app\admin\model\AuthmenuModel;
use app\admin\model\AuthroleModel;
use think\Controller;
use think\Config;
use think\Loader;
use think\Db;

class AuthmenuController extends BaseController
{
    //主页
    public function index(){
        /*$data=Db::name("auth_menu")->order("sort asc")->select();
        $result=to_tree($data);
        $this->assign("res",json_encode($result));*/

        if (request()->isAjax()){
            $data=Db::name("auth_menu")->field("id,parent_id as pid,name as title,status,url,type,ico,sort")->order("sort ASC")->select();
            $result["data"]=$data;
            $result["code"]=1;
            return json($result);
        }
        return $this->fetch("index");
    }

//    菜单的添加
    public function add(){
        if (request()->isPost()){
            $list=request()->post();
            if (empty($list["parent_id"])){
                $list["type"]="menu";  //菜单栏控制
                $list["ico"]="fa-".input("ico");
                $list["parent_id"]=0;
                $list["url"]="#";
            }elseif ($list["parent_id"]!=0){
                $find=Db::name("auth_menu")->where("id","=",$list["parent_id"])->find();
                if ($find["parent_id"]==0){
                    $list["type"]="menu";  //菜单栏控制
                }else{
                    $list["type"]="per";  //节点栏控制
                }
            }

            $findname=Db::name("auth_menu")->where("name","=",$list["name"])->find();
            if (!empty($findname)){
                return json(["code"=>0,"msg"=>"该菜单名称已存在"]);
            }
            $result=Db::name("auth_menu")->insert($list);
            if ($result){
                return json(["code"=>1,"msg"=>"添加成功"]);
            }else{
                return json(["code"=>0,"msg"=>"添加失败"]);
            }
        }
        $id=input("param.id");
        $find=Db::name("auth_menu")->where(["id"=>$id])->find();
        $this->assign("find",$find);
        return $this->fetch("add");
    }

    /*
     * 编辑
     */
    public function edit(){
        $id=input("param.id");
        if (request()->isPost()){
            $list=request()->post();
            $result=Db::name("auth_menu")->where("id","=",$list["id"])->update($list);
            if ($result){
                return json(["code"=>1,"msg"=>"编辑成功"]);
            }else{
                return json(["code"=>0,"msg"=>"编辑失败"]);
            }
        }
        $where["id"]=["=",$id];
        $find=Db::name("auth_menu")->where($where)->find();
        $s_find=Db::name("auth_menu")->where("id","=",$find["parent_id"])->field("name")->find();
        $this->assign("find",$find);
        $this->assign("s_find",$s_find);


        return $this->fetch("edit");
    }

    /*
     * 删除
     */
    public function del(){
        $id=input("param.id");
        $model=new AuthmenuModel();
        $data=$model->del_menu($id);
        writelog("删除菜单成功");
        return json($data);
    }


}